package viewCollection;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.LineBorder;

import java.util.Vector;

import util.Messages;
import viewCollection.ButtonPanel.ButtonEnum;

import dataCollection.Dice;
import dataCollection.Tree;
import dataCollection.AbstractDetail;

public class View extends JFrame {

  private static final long serialVersionUID = 5841774656315701832L;
    private int x_;
    private int y_;
    //public JButton buttons[];
    private ButtonPanel button_panel_;
    private InfoPanel info_panel_;
    private JPanel main_panel_;
    private JScrollPane scroll_pane_;
    private GridBagLayout layout_mgr_;
    private TraitsView traits_view_;
    private AptitudesView apts_view_;
    

    public View(Tree<AbstractDetail> ability_tree) {
      x_ = 800;
      y_ = 700;
      this.setTitle(Messages.getString("Title"));
      this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      this.setSize( x_, y_ );
      layout_mgr_ = new GridBagLayout();

      //this.setLayout(null);
      this.setLayout(layout_mgr_);
      info_panel_ = new InfoPanel();
      setButtons();
      setMenu();
      setMainPanel();                              //x,y
      traits_view_ = new TraitsView(main_panel_, info_panel_, ability_tree);
      apts_view_ = new AptitudesView(main_panel_);
      scroll_pane_ = new JScrollPane(main_panel_);
      scroll_pane_.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
      scroll_pane_.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
      int height = (int)main_panel_.getPreferredSize().getHeight();
      scroll_pane_.getVerticalScrollBar().setValue(height);
      scroll_pane_.setPreferredSize(main_panel_.getPreferredSize());
      // setSizes(main_panel_, scroll_pane_);
      addComponent(this, layout_mgr_, button_panel_, 0,0, 2,1, 0,0);
      addComponent(this, layout_mgr_, scroll_pane_,  1,0, 1,1, 1.0, 0.8);
      addComponent(this, layout_mgr_, info_panel_.getPanel(),1,1, 1,0, 1.0,0.2);
      
      this.setVisible(true);
    }

    
    public void setButtons() {
      button_panel_ = new ButtonPanel();
      info_panel_.setInfoText(button_panel_.getButton(ButtonEnum.TRAITS), Messages.getString("ButDesc."+0));
      this.add(button_panel_);
    }
    
    public void setMenu() {
      JMenuBar mbar = new JMenuBar();
      JMenu m1 = new JMenu( Messages.getString("View.6") );
      JMenuItem it = new JMenuItem("Test");
      it.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          System.out.println("TESTAUSGABE");
        }
      });
      m1.add(it);
      mbar.add( m1 );
      mbar.add( new JMenu(Messages.getString("View.7")) );
      mbar.add( new JMenu(Messages.getString("View.8")) );
      this.setJMenuBar( mbar );
    }
    
    public void setMainPanel() {
      main_panel_ = new JPanel();
      main_panel_.setLayout(new GridLayout(0,1,3,3));
      main_panel_.setAlignmentX(TOP_ALIGNMENT);
      main_panel_.setBorder(LineBorder.createGrayLineBorder());
      
      main_panel_.setVisible(true);
    }
    
    static void addComponent( JFrame cont,
        GridBagLayout gbl,
        Component c,
        int x, int y,
        int width, int height,
        double weightx, double weighty )
    {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.BOTH;
      gbc.gridx = x; gbc.gridy = y;
      gbc.gridwidth = width; gbc.gridheight = height;
      gbc.weightx = weightx; gbc.weighty = weighty;
      gbl.setConstraints( c, gbc );
      cont.add( c );
    }
    
    static void addComponentpane( JFrame cont,
        GridBagLayout gbl,
        Component c,
        int x, int y,
        int width, int height,
        double weightx, double weighty )
    {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.fill = GridBagConstraints.BOTH;
      gbc.anchor = GridBagConstraints.EAST;
      gbc.gridx = x; gbc.gridy = y;
      gbc.gridwidth = width; gbc.gridheight = height;
      gbc.weightx = weightx; gbc.weighty = weighty;
      gbl.setConstraints( c, gbc );
      cont.add( c );
    }   
    
    public void setTraitButtonListener(ActionListener e) {
      button_panel_.setButtonAction(ButtonEnum.TRAITS, e);
    }

    public void setAptitudesButtonListener(ActionListener e) {
      System.out.println("DERP");
      button_panel_.setButtonAction(ButtonEnum.APTITUDES, e);
    }

    public void paintTraits(Vector<Dice> dice_vec) {
      traits_view_.paintTraits(dice_vec);
    }
    
    public void paintAptitudes() {
      apts_view_.paintApts();
    }

    public TraitsView getTraitsView() {
      return traits_view_;
    }
}
